package cn.legend.travel.admin.content.controller;

import cn.legend.travel.admin.content.pojo.param.GuideIssueParam;
import cn.legend.travel.admin.content.service.IGuideService;
import cn.legend.travel.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "攻略管理模块")
@RestController
@RequestMapping("guides")
@RequiredArgsConstructor
public class GuideController {
    private final IGuideService service;

    /**
     * 查看待审核或者审核通过的文章列表
     *
     * @param pageNum 页码
     * @return 返回JsonResult封装的分页结果
     */
    @GetMapping("list")
    @ApiOperation("查看攻略列表功能")
    @PreAuthorize("hasAuthority('/articles/manage')")
    public JsonResult list(Integer pageNum) {
        return JsonResult.ok(service.list(pageNum == null ? 1 : pageNum));
    }

    @PostMapping("check")
    @ApiOperation("审核功能")
    @PreAuthorize("hasAuthority('/articles/manage')")
    public JsonResult check(@RequestParam Long id, @RequestParam @Range(min = 1, max = 2, message = "请合法发送发请求") Integer state) {
        service.check(id, state);
        return JsonResult.ok();
    }

    @PostMapping("changeDisplay")
    @ApiOperation("改变文章是否显示功能")
    @PreAuthorize("hasAuthority('/articles/manage')")
    public JsonResult changeDisplay(@RequestParam Long id, @RequestParam @Range(min = 0, max = 1, message = "请合法发送发请求") Integer isDisplay) {
        service.changeDisplay(id, isDisplay);
        return JsonResult.ok();
    }

    @GetMapping("detail")
    @ApiOperation("查看攻略详情功能")
    @PreAuthorize("hasAuthority('/articles/manage')")
    public JsonResult getDetail(@RequestParam Long id) {
        return JsonResult.ok(service.getDetail(id));
    }


    @PostMapping("issue")
    @ApiOperation("发表官方攻略")
    @PreAuthorize("hasAuthority('/articles/issue')")
    public JsonResult issueArticle(@RequestBody GuideIssueParam param) {
        service.issue(param);
        return JsonResult.ok();
    }
}
